// *** ADDED BY HEADER FIXUP ***
#include <wx/utils.h>
// *** END ***
#include "ConnectionDlg.h"
#include <wx/xml/xml.h>
#include <wx/sstream.h>
#include <wx/sckstrm.h>
//(*InternalHeaders(ConnectionDlg)
#include <wx/intl.h>
#include <wx/string.h>
//*)
#include <wx/msgdlg.h>
#include <wx/textfile.h>
#include "Mysocket.h"
//(*IdInit(ConnectionDlg)
const long ConnectionDlg::ID_STATICTEXT3 = wxNewId();
const long ConnectionDlg::ID_TEXTCTRL3 = wxNewId();
const long ConnectionDlg::ID_STATICTEXT1 = wxNewId();
const long ConnectionDlg::ID_TEXTCTRL1 = wxNewId();
const long ConnectionDlg::ID_STATICTEXT2 = wxNewId();
const long ConnectionDlg::ID_TEXTCTRL2 = wxNewId();
const long ConnectionDlg::ID_BUTTON1 = wxNewId();
const long ConnectionDlg::ID_CHECKBOX1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(ConnectionDlg,wxDialog)
	//(*EventTable(ConnectionDlg)
	//*)
END_EVENT_TABLE()

ConnectionDlg::ConnectionDlg(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
	BuildContent(parent,id,pos,size);
}

void ConnectionDlg::BuildContent(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
	//(*Initialize(ConnectionDlg)
	wxBoxSizer* BoxSizer1;

	Create(parent, id, _("服务器连接"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxCLOSE_BOX|wxDIALOG_NO_PARENT, _T("id"));
	SetClientSize(wxDefaultSize);
	Move(wxDefaultPosition);
	BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
	StaticText3 = new wxStaticText(this, ID_STATICTEXT3, _("你的名字:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT3"));
	BoxSizer1->Add(StaticText3, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	txName = new wxTextCtrl(this, ID_TEXTCTRL3, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER, wxDefaultValidator, _T("ID_TEXTCTRL3"));
	BoxSizer1->Add(txName, 1, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	StaticText1 = new wxStaticText(this, ID_STATICTEXT1, _("服务器地址:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
	BoxSizer1->Add(StaticText1, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	txAddr = new wxTextCtrl(this, ID_TEXTCTRL1, _("127.0.0.1"), wxDefaultPosition, wxSize(97,22), wxTE_PROCESS_ENTER, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	BoxSizer1->Add(txAddr, 1, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	StaticText2 = new wxStaticText(this, ID_STATICTEXT2, _("端口:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT2"));
	BoxSizer1->Add(StaticText2, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	txPort = new wxTextCtrl(this, ID_TEXTCTRL2, _("9800"), wxDefaultPosition, wxSize(46,22), wxTE_PROCESS_ENTER, wxDefaultValidator, _T("ID_TEXTCTRL2"));
	BoxSizer1->Add(txPort, 0, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	bnConnect = new wxButton(this, ID_BUTTON1, _("连接"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
	BoxSizer1->Add(bnConnect, 0, wxALL|wxALIGN_LEFT|wxALIGN_BOTTOM, 5);
	cbAddMsg = new wxCheckBox(this, ID_CHECKBOX1, _("添加语料库"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_CHECKBOX1"));
	cbAddMsg->SetValue(false);
	BoxSizer1->Add(cbAddMsg, 0, wxALL|wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
	SetSizer(BoxSizer1);
	BoxSizer1->Fit(this);
	BoxSizer1->SetSizeHints(this);

	Connect(ID_TEXTCTRL3,wxEVT_COMMAND_TEXT_ENTER,(wxObjectEventFunction)&ConnectionDlg::OnbnConnectClick);
	Connect(ID_TEXTCTRL1,wxEVT_COMMAND_TEXT_ENTER,(wxObjectEventFunction)&ConnectionDlg::OnbnConnectClick);
	Connect(ID_TEXTCTRL2,wxEVT_COMMAND_TEXT_ENTER,(wxObjectEventFunction)&ConnectionDlg::OnbnConnectClick);
	Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&ConnectionDlg::OnbnConnectClick);
	//*)
	LoadSetting();
}

ConnectionDlg::~ConnectionDlg()
{
	//(*Destroy(ConnectionDlg)
	//*)
}

wxSocketBase* ConnectionDlg::GetConnection(wxWindow *parent)
{
    SetParent(parent);
    if(ShowModal()==EXIT_LEGAL)
        return sock;
    else return NULL;
}

enum ConnectExcept
{
	ConnectFial,
	TransactionFail,
	HasLogin
};

void ConnectionDlg::OnbnConnectClick(wxCommandEvent& event)
{
	if(txName->GetValue().IsEmpty())
	{
		wxMessageBox(_("名字不能为空!!"));
		return;
	}
	try
	{
		wxIPV4address addr;
		addr.Hostname(txAddr->GetValue());
		addr.Service(txPort->GetValue());
		sock = new wxSocketClient();
		sock->Connect(addr,false);
		sock->WaitOnConnect(5);
		if(!sock->IsConnected()) throw ConnectExcept(ConnectFial);
		else
		{
			//sock->SetTimeout(1);
			sock->SetFlags(wxSOCKET_WAITALL);
			wxString msg = wxT("<Login>")+txName->GetValue()+wxT("</Login>");
			if(!Mysocket::SendString(sock,msg)) throw ConnectExcept(TransactionFail);
			if(!Mysocket::ReceiveString(sock,msg)) throw ConnectExcept(TransactionFail);
			wxStringInputStream rev(msg);
			wxXmlDocument doc(rev);
			if(!doc.IsOk()) throw ConnectExcept(TransactionFail);
			wxXmlNode *node = doc.GetRoot();
			//wxMessageBox(wxString(buff,wxConvLibc));
		   // wxMessageBox(node->GetName()+_(" ")+node->GetChildren()->GetContent());
			if(node->GetName()==wxT("Exit")) throw ConnectExcept(HasLogin);
			else if(node->GetName()==_("Login"))
			{
				serverName = node->GetChildren()->GetContent();
				//wxMessageBox(_("机器人：")+serverName);
				SaveSetting();
				EndModal(EXIT_LEGAL);
			}
		}
	}
	catch(const ConnectExcept& e)
	{
		sock->Destroy();
		switch(e)
		{
			case ConnectFial: wxMessageBox(_("连接服务器失败！！"));break;
			case HasLogin: wxMessageBox(_("用户已连接!!"));break;
			case TransactionFail: wxMessageBox(_("客户端版本不对！！！"));break;
		}
	}
}
wxString ConnectionDlg::GetUserName()
{
	return txName->GetValue();
}
wxString ConnectionDlg::GetServerName()
{
	return serverName;
}

bool ConnectionDlg::IsAddMsg()
{
	return cbAddMsg->GetValue();
}



bool ConnectionDlg::LoadSetting()
{
	wxTextFile file(_("setting"));
	if(file.Exists())
	{
		file.Open();
		txName->SetValue(file.GetFirstLine());
		txAddr->SetValue(file.GetNextLine());
		txPort->SetValue(file.GetNextLine());
		long int t = 1;
		file.GetNextLine().ToLong(&t);
		cbAddMsg->SetValue(t==1);
		file.Close();
		return true;
	}
	return false;
}

void ConnectionDlg::SaveSetting()
{
	wxTextFile file(_("setting"));
	if(file.Exists()) file.Open(),file.Clear();
	else file.Create();
	file.AddLine(txName->GetValue());
	file.AddLine(txAddr->GetValue());
	file.AddLine(txPort->GetValue());
	file.AddLine(wxString::Format(wxT("%d"),cbAddMsg->GetValue()));
	file.Write();
}
